//
//  HLTopChannelView.m
//  HLMusicBox
//
//  Created by hulinhu on 2017/12/28.
//  Copyright © 2017年 hulinhu. All rights reserved.
//

#import "HLTopChannelView.h"

@interface HLTopChannelView ()
@property (nonatomic, weak) UILabel *lastSelectedLabel;
@property (nonatomic, strong) UIView *indicatorView;
@end


static CGFloat kTitleLabelNorimalFont = 16;
static CGFloat kTitleLabelSelectedFont = 20;
static CGFloat kAddChannelWidth = 30;
static CGFloat kSliderViewWidth = 20;
static CGFloat buttonWidth = 70;


@implementation HLTopChannelView


#pragma mark 初始化View
- (instancetype)initWithFrame:(CGRect)frame {
    if (self= [super initWithFrame:frame]) {
        [self setupSubViews];
    }
    return self;
}

#pragma mark 初始化子控件
- (void)setupSubViews {
    self.alpha = 0.8;
    // 添加subView
    [self addSubview:self.scrollView];
    self.indicatorView.frame = CGRectMake(0, self.height-2, buttonWidth-20 , 2);
    [self.scrollView addSubview:self.indicatorView];
}

#pragma mark
#pragma mark - 数值绑定
-(void)setChannelNameArray:(NSArray *)channelNameArray {
    _channelNameArray = channelNameArray;
    self.scrollView.contentSize = CGSizeMake(buttonWidth * channelNameArray.count, 0);
    for (NSInteger i = 0; i < channelNameArray.count; i++) {
        UILabel *slabel = [self createChannelLabels:100+i];
        slabel.tag = 100+i;
        slabel.frame = CGRectMake(i*buttonWidth, 0, buttonWidth, self.height);
        slabel.text = channelNameArray[i];
        [self.scrollView addSubview:slabel];
    }
    [self.scrollView bringSubviewToFront:self.indicatorView];

    // 初始绑定
    [self handleClickEvents:100];
}

-(UILabel *)createChannelLabels:(NSInteger)tag {
    
    UILabel *slabel = [GFControl createLabelWithFrame:CGRectZero text:@"" textAlignment:NSTextAlignmentCenter fontSize:kTitleLabelNorimalFont textColor:UIColorHex(000000) bgColor:UIColorHex(FFFFFF) cornerRadius:0 tapAction:^{
        [self handleClickEvents:tag];
    }];
    return slabel;
    
}

-(void)handleClickEvents:(NSInteger)tag {
    
    // 设置上一个 字体大小
    self.lastSelectedLabel.font = [UIFont systemFontOfSize:kTitleLabelNorimalFont];
    UILabel *slabel =  (UILabel *)[self.scrollView viewWithTag:tag];
    self.lastSelectedLabel = slabel;
    
    CGFloat newOffsetX = slabel.centerX - [UIScreen mainScreen].bounds.size.width*0.5;
    if (newOffsetX < 0) {
        newOffsetX = 0;
    }
    if (newOffsetX > self.scrollView.contentSize.width - self.scrollView.frame.size.width) {
        newOffsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
    }

    [UIView animateWithDuration:0.25 animations:^{
        slabel.font = [UIFont systemFontOfSize:kTitleLabelSelectedFont];
        [self.scrollView setContentOffset:CGPointMake(newOffsetX, 0)];
        //indicatorView宽度会比titleLabel宽20，centerX与titleLabel相同
        self.indicatorView.left = 10+(tag-100)*slabel.width;
    }];
    
    // 判断代理信号是否有值
    NSInteger index = tag-100;
    if (self.delegateSignal) {
        [self.delegateSignal sendNext:@(index)];
    }
    
}
- (void)selectChannelButtonWithIndex:(NSInteger)index {
     self.indicatorView.hidden = NO;
     [self handleClickEvents:index+100];
}

#pragma mark
#pragma mark - 懒加载

- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, 0, KScreenWidth, self.height);
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}
-(UIView *)indicatorView {
    
    if (!_indicatorView) {
        _indicatorView =  [GFControl createUIViewWithFrame:CGRectZero
                                                   bgColor:[UIColor purpleColor]];
    }
    return _indicatorView;
}

-(RACSubject *)delegateSignal {
    if (!_delegateSignal) {
        _delegateSignal = [RACSubject subject];
    }
    return _delegateSignal;
}

@end
